<?php
/**
 * PHP Framework emission code proxy class
 *
 * @author Andrew <andrew@vitche.com>
 * @author Frozen Rain <frozenrain@mail.ru>
 **/
class CodeProxy {

    private static $_uri = null;

    public static function initialize($uri) {

        CodeProxy::$_uri = $uri;

        $configurationPath1 = './Framework/Core/Configuration/Configuration.php';
        $configurationPath2 = './Framework.Core/Configuration/Configuration.php';
        if (file_exists($configurationPath1)) {
            require_once($configurationPath1);
        } else if (file_exists($configurationPath2)) {
            require_once($configurationPath2);
        } else {
            CodeProxy::load('Framework.Core.Configuration', 'Configuration');
        }

        $classRegistrarPath1 = './Framework/Core/Reflection/Emit/ClassRegistrar.php';
        $classRegistrarPath2 = './Framework.Core/Reflection/Emit/ClassRegistrar.php';
        if (file_exists($classRegistrarPath1)) {
            require_once($classRegistrarPath1);
        } else if (file_exists($classRegistrarPath2)) {
            require_once($classRegistrarPath2);
        } else {
            CodeProxy::load('Framework.Core.Reflection.Emit', 'ClassRegistrar');
        }

        $namingConventionMapperPath1 = './Framework/Core/Reflection/Emit/NamingConventionMapper.php';
        $namingConventionMapperPath2 = './Framework.Core/Reflection/Emit/NamingConventionMapper.php';
        if (file_exists($namingConventionMapperPath1)) {
            require_once($namingConventionMapperPath1);
        } else if (file_exists($namingConventionMapperPath2)) {
            require_once($namingConventionMapperPath2);
        } else {
            CodeProxy::load('Framework.Core.Reflection.Emit', 'NamingConventionMapper');
        }
    }

    public static function load($namespace, $class) {

        $data = file_get_contents(CodeProxy::$_uri .'?EmitWebService/get&namespace=' . $namespace . '&class=' . $class);
        $data = str_replace('<' . '?php', '', $data);
        $data = str_replace('?' . '>', '', $data);
        eval($data);
    }
}
?>